import pygame


class TextBox:
    def __init__(self, w, h, x, y):
        """
        :param w:文本框宽度
        :param h:文本框高度
        :param x:文本框坐标
        :param y:文本框坐标
        :param font:文本框中使用的字体
        :param callback:在文本框按下回车键之后的回调函数
        """
        self.width = w
        self.height = h
        self.x = x
        self.y = y
        self.text = ""  # 文本框内容
        # 创2建
        # self.__surface = pygame.Surface((w, h))
        # 如果font为None,那么效果可能不太好，建议传入font，更好调节
        self.font = pygame.font.Font("font/方正舒体__GBK.ttf", 22)  # 使用pygame自带字体
        self.show_text = ""

    def draw(self, dest_surf):
        text_surf = self.font.render(self.show_text, True, (0,0,0))
        # dest_surf.blit(self.__surface, (self.x, self.y))
        dest_surf.blit(text_surf, (self.x, self.y + (self.height - text_surf.get_height())),
                       (0, 0, self.width, self.height))

    def key_down(self, event):

        unicode = event.unicode
        key = event.key

        if key == 1073742049:
            print("shift")
            return
        # 退位键
        if key == 8:
            self.text = self.text[:-1]
            self.show_text = self.text
            return

        # 切换大小写键
        if key == 301:
            return



        if unicode != "":
            char = unicode
        else:
            char = chr(key)

        self.text += char
        self.show_text = self.text



class PassWord_textbox(TextBox):
    def __init__(self,w, h, x, y):
        TextBox.__init__(self,w, h, x, y)

    def key_down(self, event):
        TextBox.key_down(self,event)
        a = len(self.text)
        self.show_text = a * '*'
